package 左哥算法.compare.贪心;

import java.util.Random;
import java.util.function.Function;

public class Ch02括号深度 {

    public static boolean isValid(char[] str) {
        if (str == null || str.equals("")) {
            return false;
        }
        int status = 0;
        for (int i = 0; i < str.length; i++) {
            if (str[i] != ')' && str[i] != '(') {
                return false;
            }
            if (str[i] == ')' && --status < 0) {
                return false;
            }
            if (str[i] == '(') {
                status++;
            }
        }
        return status == 0;
    }

    public static int deep(String s) {
        char[] str = s.toCharArray();
        if (!isValid(str)) {
            return 0;
        }
        int count = 0;
        int max = 0;
        for (int i = 0; i < str.length; i++) {
            if (str[i] == '(') {
                max = Math.max(max, ++count);
            } else {
                count--;
            }
        }
        return max;
    }

    public static int maxLength(String str) {
        if (str == null || str.equals("")) {
            return 0;
        }
        char[] chas = str.toCharArray();
        int[] dp = new int[chas.length];
        int pre = 0;
        int res = 0;
        for (int i = 1; i < chas.length; i++) {
            if (chas[i] == ')') {
                pre = i - dp[i - 1] - 1;
                if (pre >= 0 && chas[pre] == '(') {
                    dp[i] = dp[i - 1] + 2 + (pre > 0 ? dp[pre - 1] : 0);
                }
            }
            res = Math.max(res, dp[i]);
        }
        return res;
    }

    public static void main(String[] args) {
        String test = "((()))";
        System.out.println(deep(test));

    }
    public static void compare(Function<String, Integer> fun){
        int count=100000;
        Random r=new Random();
        while (count-- > 0) {
            StringBuilder builder=new StringBuilder();
            int len = r.nextInt(50) * 2;
            while (len-- > 0) {
                builder.append(r.nextBoolean()? '(' : ')');
            }
            Integer apply = fun.apply(builder.toString());
            int ans = deep(builder.toString());
            if (!apply.equals(ans)) {
                System.out.println(builder.toString());
                System.out.println("错误"+apply);
                System.out.println("正确"+ans);
            }
        }
        System.out.println("测试结束");
    }

}
